﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WeifenLuo.WinFormsUI.Docking;
using CustomUIControls;


namespace SalesVMT
{
    public partial class frmProviderDebitList : DockContent
    {
        int pageSize = UIParamater.PageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int providerID=0;
        public frmProviderDebitList()
        {
            InitializeComponent();
            DisplayFrame();
            UISetting.LoadProviderFromReceipt(ref cboProvider, "NCC",0);
        }

        private void DisplayFrame()
        {
            UISetting.AddColumn(ref dgvList1, "Receipt_ID", "Khóa", "", 1, false, true);
            UISetting.AddColumn(ref dgvList1, "RowNumber", "STT", "C", ColumnWidth.RowNumber, true, true);
            UISetting.AddColumn(ref dgvList1, "Receipt_Code", "Phiếu NK", "", ColumnWidth.Code3, true, true);
            UISetting.AddColumn(ref dgvList1, "Receipt_Date", "Ngày nhập", "C", ColumnWidth.Date, true, true);
            UISetting.AddColumn(ref dgvList1, "Receipt_DebitDays", "Số ngày nợ", "R", 110, true, true);
            UISetting.AddColumn(ref dgvList1, "Receipt_Total", "Tổng cộng", "R", ColumnWidth.TotalPrice, true, true);
            UISetting.AddColumn(ref dgvList1, "Receipt_Pay", "Đã trả", "R", ColumnWidth.TotalPrice, true, true);
            UISetting.AddColumn(ref dgvList1, "Remainder", "Còn nợ", "R", ColumnWidth.TotalPrice, true, true);
        }
        private void LoadData()
        {
            
            providerID = Convert.ToInt32(cboProvider.ComboBox.SelectedValue);
            DataTable _dt = ReceiptDB.GetListProviderDebit(out itemCount, providerID, currPage, pageSize).Tables[0];
            PageIndex();
            dgvList1.Rows.Clear();
            int n = _dt.Rows.Count;
            for (int i = 0; i < n; i++)
            {
                dgvList1.Rows.Insert(i, 1);
                dgvList1.Rows[i].Cells["RowNumber"].Value = i + 1;
                dgvList1.Rows[i].Cells["Receipt_ID"].Value = _dt.Rows[i]["Receipt_ID"].ToString();
                dgvList1.Rows[i].Cells["Receipt_Code"].Value = _dt.Rows[i]["Receipt_Code"].ToString();
                dgvList1.Rows[i].Cells["Receipt_Date"].Value = Convert.ToDateTime(_dt.Rows[i]["Receipt_Date"]).ToString("dd/MM/yyyy");
                dgvList1.Rows[i].Cells["Receipt_DebitDays"].Value = _dt.Rows[i]["Receipt_DebitDays"].ToString();
                dgvList1.Rows[i].Cells["Receipt_Total"].Value = Convert.ToDecimal(_dt.Rows[i]["Receipt_Total"]).ToString("#,##0");
                dgvList1.Rows[i].Cells["Receipt_Pay"].Value = Convert.ToDecimal(_dt.Rows[i]["Receipt_Pay"]).ToString("#,##0");
                dgvList1.Rows[i].Cells["Remainder"].Value = Convert.ToDecimal(Convert.ToDecimal(_dt.Rows[i]["Receipt_Total"]) - Convert.ToDecimal(_dt.Rows[i]["Receipt_Pay"])).ToString("#,##0");
            }
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }
        private void btnFirst_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage != 0)
                currPage--;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage != totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void cboProvider_SelectedIndexChanged(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void mnuPrint_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(cboProvider.ComboBox.SelectedValue) == 0)
                return;
            string ProviderID = cboProvider.ComboBox.SelectedValue.ToString();
            frmReport fReport = new frmReport("CONG NO NCC", new string[] { ProviderID, "Công nợ nhà cung cấp" });
            fReport.Show(this);
        }
    }
}